﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quest.PowerGUI.ScriptEditor.Shared;
using Infragistics.UltraChart.Core.Primitives;
using System.Windows.Forms;
using ActiproSoftware.SyntaxEditor;

namespace PowerGuiVsx.Core
{
    public class PsControlWrapper : PSSyntaxEditorControl
    {
        private string _installPath;

        public PsControlWrapper(string installPath)
        {
            _installPath = installPath;
            var syntaxFile = System.IO.Path.Combine(installPath, "PowerShellSyntax.xml");

            this.AutoCheckSyntax = true;
            this.Dock = DockStyle.Fill;
            this.Document = new ActiproSoftware.SyntaxEditor.Document();
            this.ReadOnly = false;
            this.Script = null;
            this.BracketHighlightingVisible = true;
            this.Document.LineModificationMarkingEnabled = true;
            this.WordWrapping = false;
            this.Initialize();

            this.Document.LoadLanguageFromXml(syntaxFile, 0);
        }

        public void ShowSnippet()
        {
            if (!this.ReadOnly)
            {
                base.Document.GetWordText(base.Caret.Offset);
                try
                {
                    CodeSnippetFolder csn = new CodeSnippetFolder("Root", System.IO.Path.Combine(_installPath, "snippets"));
                    base.IntelliPrompt.CodeSnippets.ShowInsertSnippetPopup(base.Caret.Offset,
                        "Insert snippet", csn, CodeSnippetTypes.Expansion);
                }
                catch
                {
                }
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Control && !e.Alt && e.KeyCode == Keys.I)
            {
                ShowSnippet();
            }
            else
            {
                base.OnKeyDown(e);
            }
        }
    }
}
